package util;

import pojo.ColDesc;
import pojo.TableDesc;
import pojo.TableExcel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
public class DocUtil {

    // 时间格式转换 (防止多线程异常)
    private final static ThreadLocal<SimpleDateFormat> dateFormatSSS = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS"));

    public static void generateExcel(List<TableDesc> tableDescList, String filePath) throws Exception {
        List<TableExcel> excelList = new ArrayList<>();
        for (TableDesc table : tableDescList) {
            for (int i = 0; i < table.getColDescs().size(); i++) {
                ColDesc colDesc = table.getColDescs().get(i);
                // 是否非空
                String empty = colDesc.getNullAble() == 1 ? "否" : "是";
                String typeName = "";
                if (colDesc.getDecimalDigits() == null || colDesc.getDecimalDigits() == 0) {
                    typeName = colDesc.getTypeName() + "(" + colDesc.getTypeSize() + ")";
                } else {
                    typeName = colDesc.getTypeName() + "(" + colDesc.getTypeSize() + "," + colDesc.getDecimalDigits() + ")";
                }
                String tabTemp = "";
                if (i == 0) {
                    tabTemp = table.getTableName() + "(" + table.getTableComment() + ")";
                }
                excelList.add(new TableExcel(tabTemp, i + 1, colDesc.getColumnName(), typeName, empty, colDesc.getRemarks()));
            }
        }
        File file = MyExcelUtil.list2File2(excelList, filePath);
        System.out.println(file.getPath()  +"          - success");
    }

    public static void generateMarkDown(List<TableDesc> tableDescList, String filePath) {
        filePath += dateFormatSSS.get().format(new Date()) +".md";
        List<String> markDownlList = new ArrayList<>();
        for (TableDesc table : tableDescList) {
            markDownlList.add("");
            markDownlList.add("### _TABLE_CN_(_TABLE_EN_)".replace("_TABLE_CN_", table.getTableComment()).replace("_TABLE_EN_", table.getTableName()));
            markDownlList.add("| 序号 | 字段名称 | 数据类型 | 是否非空 | 字段说明 |");
            markDownlList.add("| ---- | ---- | ---- | ---- | ---- |");
            for (int i = 0; i < table.getColDescs().size(); i++) {
                ColDesc colDesc = table.getColDescs().get(i);
                // 是否非空
                String empty = colDesc.getNullAble() == 1 ? "否" : "是";
                String typeName = "";
                if (colDesc.getDecimalDigits() == null || colDesc.getDecimalDigits() == 0) {
                    typeName = colDesc.getTypeName() + "(" + colDesc.getTypeSize() + ")";
                } else {
                    typeName = colDesc.getTypeName() + "(" + colDesc.getTypeSize() + "," + colDesc.getDecimalDigits() + ")";
                }
                String col = "| _SEQ_ | _EN_ | _type_ | _empty_ | _DESC_ |"
                        .replace("_SEQ_", (i + 1) + "")
                        .replace("_EN_", colDesc.getColumnName())
                        .replace("_type_", typeName)
                        .replace("_DESC_", colDesc.getRemarks())
                        .replace("_empty_", empty);
                markDownlList.add(col);
            }
        }
        System.out.println(filePath  + "          - success");
        IoUtil.writer(markDownlList, filePath);
    }
}
